package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.db.dao.AreaDao;
import com.ciban.smarthome.module.db.dao.EquipmentDao;
import com.ciban.smarthome.module.entity.Equipment;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.entity.EquiTypeInfo;
import com.ciban.smarthome.module.ui.adapter.EquipmentListAdapter;

public class E_EquipmentListActivity extends BaseActivity implements OnClickListener {
    private GridView gv_list;
    private EquipmentListAdapter adapter;
    private boolean isAddEquipment = false; // 是否添加了设备

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equip_list);
        initViews();
    }

    private void initViews() {
        ((TextView) findViewById(R.id.title)).setText("添加电器");
        ((ImageView) findViewById(R.id.title_add)).setVisibility(View.GONE);
        ((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);

        gv_list = (GridView) findViewById(R.id.area_equipments_show);

        adapter = new EquipmentListAdapter(this);
        gv_list.setAdapter(adapter);

        setListeners();
    }

    private void setListeners() {
        gv_list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                EquiTypeInfo info = (EquiTypeInfo) adapter.getItem(position);

                if (info.getType() == 6) {
                    //海康威视的摄像头
                    AreaDao dao = new AreaDao(E_EquipmentListActivity.this);
                    dao.addEquipType(6, baseApp.getNowArea().getId());


                    EquipmentDao edao = new EquipmentDao(E_EquipmentListActivity.this);
                    Equipment einfo = new Equipment();
                    einfo.setType(info.getType());
                    einfo.setName(info.getName());

                    edao.addEquipMent(einfo);

                    dao = null;

                    isAddEquipment = true;
                    return;
                }

                Intent intent;
                try {
                    intent = new Intent(E_EquipmentListActivity.this, Class
                            .forName("com.ciban.smarthome.module.ui."
                                    + info.getModifiy_view()));
                    intent.putExtra("type", info.getType());
                    startActivityForResult(intent, 102);
                } catch (ClassNotFoundException e) {
                    Toast.makeText(E_EquipmentListActivity.this, "正在添加中..", Toast.LENGTH_SHORT)
                            .show();
                }
            }

        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                if (isAddEquipment)
                    setResult(RESULT_OK);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK)
            isAddEquipment = true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isAddEquipment)
                setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
